package com.example.farmer.Adapter.Product;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.farmer.Activity.ProductDetail;
import com.example.farmer.Database.ProductPublishDatabase;
import com.example.farmer.Entity.ProductPost;
import com.example.farmer.R;

import java.util.List;

public class MyProductAdapter extends RecyclerView.Adapter<MyProductAdapter.ProductViewHolder> {
    private static List<ProductPost> productPosts;
    private static Context context;


    // 构造函数，接收帖子列表和上下文
    public MyProductAdapter(List<ProductPost> productPosts, Context context) {
        this.productPosts = productPosts;
        this.context = context;
    }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 从布局文件中加载帖子项的视图
        View postView = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_post_item, parent, false);
        return new ProductViewHolder(postView);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
        // 获取当前位置的帖子对象
        ProductPost productPost = productPosts.get(position);
        // 绑定帖子内容和图片到ViewHolder
        holder.bind(productPost);
    }

    @Override
    public int getItemCount() {
        // 返回帖子列表的大小
        return productPosts == null ? 0 : productPosts.size();
    }

    // 更新帖子数据的方法
    public void updatePosts(List<ProductPost> newProductPost) {
        if (newProductPost != null) {
            this.productPosts = newProductPost;
            notifyItemRemoved(1); // 通知数据集改变
        }
    }

    // ViewHolder内部类
    public class ProductViewHolder extends RecyclerView.ViewHolder {
        public TextView title,producttype,prices,freight;
        public ImageView productimage;
        public TextView textDelete;
        public LinearLayout linearLayout;
        // 其他视图组件...




        public ProductViewHolder(View itemView) {
            super(itemView);
            // 绑定视图组件
            productimage = itemView.findViewById(R.id.productimage);
            title = itemView.findViewById(R.id.title);
            producttype = itemView.findViewById(R.id.producttype);
            prices = itemView.findViewById(R.id.prices);
            freight = itemView.findViewById(R.id.freight);
            textDelete = itemView.findViewById(R.id.text_delete);
            linearLayout = itemView.findViewById(R.id.xiangqing);
            // 其他视图组件初始化...

            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 获取点击的商品项的 ID
                    // 获取点击位置的 ProductPost 对象
                    ProductPost productPost = productPosts.get(getAdapterPosition());
                    // 获取 ProductPost 对象的 ID
                    int productId = productPost.getId();
                    // 创建 Intent 并启动 ProductDetail，传递商品 ID
                    Intent intent = new Intent(context, ProductDetail.class);
                    intent.putExtra("PRODUCT_ID", productId);
                    context.startActivity(intent);
                }
            });


            textDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 删除帖子的逻辑
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        ProductPost productPostsToDelete = productPosts.get(position);
                        ProductPublishDatabase productPublishDatabase = ProductPublishDatabase.getInstance(context);
                        productPublishDatabase.deletePost(productPostsToDelete.getId());

                        // 从列表中移除帖子，并更新UI
                        productPosts.remove(position);
                        notifyItemRemoved(position);
                    }
                }
            });

        }

        // bind方法用于将帖子数据绑定到ViewHolder的视图上
        @SuppressLint("SetTextI18n")
        public void bind(ProductPost productPost) {
            title.setText("名称：" + productPost.getTitle());
            producttype.setText("类型：" + productPost.getProducttype());
            prices.setText("价格：" + productPost.getPrices() + "元");
            freight.setText("运费：" + productPost.getFreight()+"元");
            if (productPost.hasImage()) {
                productimage.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(productPost.getImageUri())
                        .override(400,400)
                        .into(productimage);
            } else {
//                imagePostContent.setVisibility(View.GONE);
            }
            // 绑定其他视图组件...
        }
    }
}